package goroutines_pool

import (
	"fmt"
	"math"
	"strings"
	"work_pool/bgf_log"
	"work_pool/panjf2000/ants/v2"
)

type poolLoger struct {
	bgfLogger *bgf_log.Logger
}

func (pl poolLoger) Printf(format string, args ...interface{}) {
	logStr := fmt.Sprintf(format, args...)
	logStr = strings.Replace(logStr, "\n", "\t", -1)
	pl.bgfLogger.Error(logStr)
}

var logger = &poolLoger{bgf_log.GetLogger("201110_goroutines_pool_logger")}

var antsPool *ants.Pool

//获取协程池
func GetPool() *ants.Pool {
	if antsPool == nil {
		var err error
		antsPool, err = ants.NewPool(math.MaxInt32, ants.WithLogger(logger))
		if err != nil {
			panic(fmt.Sprintf("创建ants pool异常, error:%v", err))
		}
	}
	return antsPool
}

//释放协程池
func Release() bool {
	if antsPool == nil {
		return false
	}

	antsPool.Release()
	antsPool = nil
	return true
}
